package com.lkx.web;

import com.alibaba.fastjson.JSON;
import com.lkx.dao.OrderMapper;
import com.lkx.dao.RangerOrderMapper;
import com.lkx.domain.Order;
import com.lkx.domain.RangerOrder;
import com.lkx.redis.RedisUtils;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * ClassName: IndexController
 * Function:  TODO
 * Date:      2019-06-19 14:49
 * author     likaixuan
 * version    V1.0
 */
@RestController
public class IndexController {


    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private RedisUtils redisUtils;

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private RangerOrderMapper rangerOrderMapper;

    /**使用RocketMq的生产者*/
    @Autowired
    private DefaultMQProducer defaultMQProducer;


    @GetMapping("/add")
    public int add() throws Exception {
        Order order = new Order("201902120001", "likaixuan", new Date().getSeconds());
        return orderMapper.insert(order);

        /*Order order = new Order("201902120001", "rocketMQ", new Date().getSeconds());
        //logger.info("开始发送消息：" + i);
        Message sendMsg = new Message("DemoTopic", "DemoTag", JSON.toJSONBytes(order));
        long lazyTime = System.currentTimeMillis()+10000;
        //设置延时10s
        //sendMsg.setDelayTimeLevel(10);
        //默认3秒超时
        SendResult sendResult = defaultMQProducer.send(sendMsg);*/
    }

    @GetMapping("/add1")
    public int add1(){
        int num = 10000;
        List<Integer> list = new ArrayList<>();
        long startTime = new Date().getTime();
        for(int i=0;i<num;i++) {
            list.add(i);
        }
        list.parallelStream().forEach(e->{
            Order order = new Order("201902120001", "likaixuan"+e, (int)System.currentTimeMillis()/1000);
            orderMapper.insert(order);
        });
        long endTime = new Date().getTime();
        logger.info("共用时:"+(endTime-startTime)+" ms");
        return num;
    }


    @GetMapping("/addRange")
    public int addRange(int num){
        List<Integer> list = new ArrayList<>();
        long startTime = new Date().getTime();
        for(int i=0;i<num;i++) {
            list.add(i);
        }
        list.parallelStream().forEach(e->{
            RangerOrder order = new RangerOrder("201902120001", "likaixuan"+e, (int)System.currentTimeMillis()/1000);
            rangerOrderMapper.insert(order);
        });
        long endTime = new Date().getTime();
        logger.info("共用时:"+(endTime-startTime)+" ms");
        return num;
    }

    @GetMapping("/addMq")
    public int addMq(int num) {
        List<Integer> list = new ArrayList<>();
        long startTime = new Date().getTime();
        for(int i=0;i<num;i++) {
            list.add(i);
        }
        list.parallelStream().forEach(e->{
            Order order = new Order("201902120001", "rocketMQ", (int)System.currentTimeMillis()/1000);
            Message sendMsg = new Message("DemoTopic", "DemoTag", JSON.toJSONBytes(order));
            //延时消费
            //sendMsg.setDelayTimeLevel(3);
            //默认3秒超时
            try {
                SendResult sendResult = defaultMQProducer.send(sendMsg);

            } catch (Exception e1) {
                e1.printStackTrace();
            }

        });

        long endTime = new Date().getTime();
        logger.info("发送"+num+"条数据，rocketMQ共用时:"+(endTime-startTime)+" ms");
        return num;
    }

    @GetMapping("/add3")
    @Transactional
    public int add3(){
        int num = 500000;
        long startTime = new Date().getTime();
        Order order = new Order("201902120001", "rocketMQ", new Date().getSeconds());
        for(int i=0;i<num;i++) {
            redisUtils.set("order:list:"+i,order,7200L);
        }
        long endTime = new Date().getTime();
        logger.info("redis共用时:"+(endTime-startTime)+" ms");
        return num;
    }

    @GetMapping("/addlist")
    @Transactional
    public int addlist(){
        int num = 30000;
        long startTime = new Date().getTime();
        List<Order> list = new ArrayList<>();
        for(int i=0;i<num;i++) {
            Order order = new Order("201902120001", "addlist", new Date().getSeconds());
            list.add(order);
        }
        orderMapper.batchInsert(list);
        long endTime = new Date().getTime();
        logger.info("addlist共用时:"+(endTime-startTime)+" ms");
        return num;
    }


    @GetMapping("/list")
    @Transactional
    public int list(){
        int num = 10000;
        long startTime = new Date().getTime();
        List<Long> list = new ArrayList<>();
        List<Order> result = new ArrayList<>();
        for(int i=0;i<num;i++) {
            list.add(new Long(i));
        }
        list.parallelStream().forEach(e->{
            result.add(orderMapper.selectByPrimaryKey(e));
        });
        long endTime = new Date().getTime();
        logger.info("list共用时:"+(endTime-startTime)+" ms");
        return result.size();
    }


    @GetMapping("/redislist")
    @Transactional
    public int redislist(){
        int num = 10000;
        long startTime = new Date().getTime();
        List<Integer> list = new ArrayList<>();
        List<Order> result = new ArrayList<>();
        for(int i=0;i<num;i++) {
            list.add(i);
        }
        list.parallelStream().forEach(e->{
            result.add((Order) redisUtils.get("order:list:"+e));
        });
        long endTime = new Date().getTime();
        logger.info("list共用时:"+(endTime-startTime)+" ms");
        return result.size();
    }



}
